<?php

class Planning_DocumentController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function preDispatch() {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('/user/login');
        }

        $view = new Zend_View();
        $view->headTitle(' > Planning > Files');
        $this->view->userDetail = $auth->getIdentity();
    }

    public function indexAction() {
        $documentModel = new Application_Model_DocumentModel();
        $paginator = $documentModel->getAllDocuments($this->_request);
        $this->view->paginator = $paginator;
    }

    public function listAction() {
        $this->_helper->layout->disableLayout();
        $documentModel = new Application_Model_DocumentModel();
        $paginator = $documentModel->getAllDocuments($this->_request);
        $this->view->paginator = $paginator;
    }

    public function addAction() {
        $this->_helper->layout->disableLayout();
        $form = new Application_Form_documents_AddForm();
        $this->view->form = $form;

        if ($this->_request->isPost()) {

            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $uploaded = move_uploaded_file($_FILES["doc_path"]["tmp_name"], "./uploads/files/" . $_FILES["doc_path"]["name"]);
                if ($uploaded) {
                    $data = array(
                        'name' => $this->_request->getPost('name'),
                        'uploader_id' => $this->view->userDetail->id,
                        'file' => $_FILES["doc_path"]["name"],
                        'cabinet' => $this->_request->getPost('cabinetList'),
                        'switch' => $this->_request->getPost('switchList'),
                        'status' => $this->_request->getPost('status'),
                        'date' => Zend_Date::now()->toString('yyyy-MM-dd HH:mm:ss')
                    );
                    $documentModel = new Application_Model_DocumentModel();
                    $documentModel->save($data);
                    $this->_redirect("planning/document");
                    exit;
                }
            }
        } else {
            // this line will be called if data was not submited
            //  $form->populate($formData);
        }
    }

    public function editAction() {
        $this->_helper->layout->disableLayout();
        $documentModel = new Application_Model_DocumentModel();
        $form = new Application_Form_documents_AddForm();
        $docId = $this->_request->getParam('id');
        $this->view->documentId = $docId;
        $docDetail = $documentModel->getDocument('id', $docId);
        $form->populate(array(
            'name' => $docDetail['name'],
            'cabinetList' => $docDetail['cabinet'],
            'switchList' => $docDetail['switch'],
            'status' => $docDetail['status'],
        ));
        if ($this->_request->isPost()) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $uploaded = move_uploaded_file($_FILES["doc_path"]["tmp_name"], "./uploads/files/" . $_FILES["doc_path"]["name"]);
                if ($uploaded) {
                    $data = array(
                        'id' => $docId,
                        'name' => $this->_request->getPost('name'),
                        'file' => $_FILES["doc_path"]["name"],
                        'cabinet' => $this->_request->getPost('cabinetList'),
                        'switch' => $this->_request->getPost('switchList'),
                        'status' => $this->_request->getPost('status')
                    );
                    $documentModel = new Application_Model_DocumentModel();
                    $documentModel->save($data);
                    $this->_redirect("planning/document");
                    exit;
                }
            }
        } else {
            
        }
        $this->view->form = $form;
    }

    public function downloadAction() {

        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        $documentName = $this->_request->getParam('id');
       
        // Now make sure the file actually exists
        if (!file_exists("./uploads/files/$documentName")) {
            echo("File does not exist");
            exit;
        }

        header("Content-disposition: attachment; filename=$documentName");
        header("Content-type: application/octet-stream");
        readfile("./uploads/files/$documentName");        
        exit(0);
    }

}